﻿/*-------------------------------------
//
//CopyLeft 2007 开发二组 
//
//文件名：InvoiceDetail.cs
//
//文件功能描述：实现对员工的相关操作.
// 
//创建标识：2007.11.21 曾健铭
//
//修改标识: 2007.11.28 曾健铭
//修改描述: 修正Bug.按规范重新编码
//------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using JXMSTC.SmartBusiness.BFL.IBLL;
using JXMSTC.SmartBusiness.BFL.BLLFactory;
using JXMSTC.SmartBusiness.Common.Model;
using System.Text.RegularExpressions;
using localhost;

public partial class UserControls_InvoiceDetail : System.Web.UI.UserControl
{
    /// <summary>
    /// 起始页面,加载发货单信息(订单项信息,订单信息,收货人信息)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string strInvoiceID = string.Empty;
            try
            {
                strInvoiceID = Request["InvoiceID"].ToString();
                string strPattern = @"INV-\d{13}";//采用正则表达式验证
                if ((strInvoiceID.Length !=17) || (!Regex.Match(strInvoiceID, strPattern).Success))
                {
                    strInvoiceID = string.Empty;//验证失败
                }
            }
            catch
            {
                strInvoiceID = string.Empty;
            }
            if (strInvoiceID == string.Empty)
            {
                lblInfo.Text ="传入的发货单ID有错误";
            }
            else
            {
                //保存发货单号
                hdfInvoiceID.Value = strInvoiceID;

                //绑定数据到Label中
                BindDataToLabel(strInvoiceID);

                string strOrderInfoID = Session["OrderInfoID"].ToString();

                hdfOrderID.Value = strOrderInfoID;

                //由订单号获取订单项

                DocumentService documentService = new DocumentService();
                
                //
                //配置分页参数
                //
                int iPageSize = 10;
                int iPageIndex = 1;

                //
                //查询订单项，并绑定
                //
                DataSet ds = documentService.GetOrderItemForPaging(iPageSize, iPageIndex, strOrderInfoID);
                if (ds != null)
                {
                    GridviewBind(ds);
                }

                //
                //隐藏控件保存共有多少条记录
                //
                int iItems = documentService.GetOrderItemRows(strOrderInfoID);
                hdfRecords.Value = iItems.ToString();

                //
                //绑定分页按钮
                //
                BindToPaging(iPageSize, iPageIndex);
            }
                        
        }
    }
    /// <summary>
    /// 取消按钮的响应,返回到发货单管理页面.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnCancel_Click(object sender, ImageClickEventArgs e)
    {
        string strUrl = "InvoiceManage.aspx";//设置发货单管理页面的URL.
        Server.Transfer(strUrl,true);

    }
    /// <summary>
    /// 发货
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnSave_Click(object sender, ImageClickEventArgs e)
    {
        //获取发货单号和发货人ID
        string strInvoiceID = hdfInvoiceID.Value;
        int iStoreKeeperID = Convert.ToInt32(Session["StaffID"]);
        //发货
        bool blnResult = (new localhost.DocumentService()).UpdateConsignmentStatus(strInvoiceID, iStoreKeeperID);
        if (blnResult)
        {
            //发货成功
            ibtnSave.Visible = false;
            lblInvoiceStatus.Text = "已发货";//修改显示信息
        }
        else
        {
            //发货失败
            ibtnSave.Visible = true;
        }
    }
    /// <summary>
    /// 由发货单号获取发货单的信息,将数据绑定到Label
    /// </summary>
    /// <param name="strInvoiceID"></param>
    private void BindDataToLabel(string strInvoiceID)
    {
        //localhost.V_Invoice invoice = new localhost.V_Invoice();
       // invoice = (new localhost.DocumentService()).GetInvoiceDetail(strInvoiceID);
        localhost.DocumentService docService = new localhost.DocumentService();
        localhost.V_Invoice  invoice=docService.GetInvoiceDetail(strInvoiceID);

        if (invoice == null)
        {
            lblInfo.Text = "发货单不存在";
        }
        else
        {
            //收货人信息.
            lblName.Text = invoice.ShipToFirstName + invoice.ShipToLastName;//姓名
            lblCity.Text = invoice.ShipCity;//城市
            lblZip.Text = invoice.ShipZip;//邮编
            lblAddress.Text = invoice.ShipAddress;//地址
            lblProvince.Text = invoice.ShipProvince;//省份
            //收货单信息.
            lblInvoiceID.Text = invoice.InvoiceID;//发货单号
            Session["InvoiceID"] = lblInvoiceID.Text;

            lblOrderID.Text = invoice.OrderInfoID;//订单号
            Session["OrderInfoID"] = lblOrderID.Text;

            //lblBillName.Text = invoice.
            int iStoreKeeper = invoice.StorekeeperID;
            lblStoreKeeper.Text = (new localhost.StaffService()).GetStaffNameByID(iStoreKeeper);//发货人姓名
            lblSendStyle.Text = invoice.ShipMethod;//配送方式
            lblOrderDate.Text = invoice.CreationTime.ToShortDateString();//下单日期
            lblTotalPrice.Text = (new localhost.DocumentService()).GetOrderDetail(lblOrderID.Text).TotalPrice.ToString();//发货单总价
            //判断发货单状态
            if (invoice.ConsignmentStatus)
            {
                lblInvoiceStatus.Text = "已发货";
                //如果已发货,则将发货按钮设置为不可用.
                ibtnSave.Visible = false;
            }
            else
            {
                lblInvoiceStatus.Text = "未发货";
                ibtnSave.Visible = true;
            }
        }
    }
  

    private void GridviewBind(DataSet ds)
    {
        this.gdvInvoice.DataSource = ds.Tables[0].DefaultView;
        this.gdvInvoice.DataBind();
    }

    #region //绑定分页按钮
    void BindToPaging(int iPageSize, int iPageIndex)
    {
        DocumentService documentService = new DocumentService();
        //
        //读取订单数
        //
        int iOrders = Convert.ToInt32(hdfRecords.Value.Trim());

        if (iOrders > 0)
        {
            //
            //有订单
            //

            //
            //订单数
            //
            lblRecordCount.Text = "共 " + iOrders.ToString() + " 条";
            lblPageCount.Visible = true;
            lblNoRecord.Visible = false;

            //
            //计算总页数
            //
            int iPages = iOrders / iPageSize;
            if (iOrders % iPageSize != 0)
            {
                iPages += 1;
            }//if(iOrders % iPageSize != 0)
            lblPageCount.Text = "共 " + iPages.ToString() + " 页";
            lblPageCount.Visible = true;

            //
            //有多页
            //
            if (iPages > 1)
            {
                btnFirst.Visible = true;
                btnPrev.Visible = true;
                btnNext.Visible = true;
                btnLast.Visible = true;
            }
            else
            {
                btnFirst.Visible = false;
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;
            }//if (iPages > 1)

            //
            //当前页
            //
            lblCurrentIndex.Text = "第 " + iPageIndex + " 页";
            lblCurrentIndex.Visible = true;
        }
        else
        {
            //
            //没有订单
            //
            lblNoRecord.Visible = true;
            lblPageCount.Visible = false;
            lblRecordCount.Visible = false;
            lblCurrentIndex.Visible = false;

            btnFirst.Visible = false;
            btnPrev.Visible = false;
            btnNext.Visible = false;
            btnLast.Visible = false;

        }//if(iOrders > 0)
    }

    #endregion

    #region //翻页
    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        //
        //获取当前页
        //
        string strPageIndex = lblCurrentIndex.Text.Trim();
        strPageIndex = strPageIndex.Substring(2);
        strPageIndex = strPageIndex.Substring(0, strPageIndex.Length - strPageIndex.LastIndexOf(" "));
        int iPageIndex = Convert.ToInt32(strPageIndex);

        //
        //获取总页数
        //
        string strPageCount = lblPageCount.Text.Trim();
        strPageCount = strPageCount.Substring(2);
        strPageCount = strPageCount.Substring(0, strPageCount.Length - strPageCount.LastIndexOf(" "));
        int iPageCount = Convert.ToInt32(strPageCount);

        //
        //判断操作类型
        //
        string arg = ((LinkButton)sender).CommandArgument.ToString();
        switch (arg)
        {
            case "prev"://上一页
                if (iPageIndex > 1)
                {
                    iPageIndex -= 1;
                }
                break;
            case "next"://下一页
                if (iPageIndex < iPageCount)
                {
                    iPageIndex += 1;
                }
                break;
            case "last"://最后一页
                iPageIndex = iPageCount;
                break;
            default://首页
                iPageIndex = 1;
                break;
        }//switch (arg)

        //
        //配置查询条件
        //
        int iPageSize = 10;
        string strOrderID = hdfOrderID.Value.Trim();

        //
        //绑定下一页的数据
        //
        DataSet ds = (new DocumentService()).GetOrderItemForPaging(iPageSize, iPageIndex, strOrderID);
        GridviewBind(ds);

        //
        //绑定分页按钮
        //
        BindToPaging(iPageSize, iPageIndex);
    }
    #endregion
}
